package animator.phantom.controller;

/*
    Copyright Janne Liljeblad

    This file is part of Phantom2D.

    Phantom2D is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Phantom2D is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Phantom2D.  If not, see <http://www.gnu.org/licenses/>.
*/

import animator.phantom.renderer.threedee.*;
import animator.phantom.renderer.RenderNode;
import animator.phantom.gui.modals.*;
import java.util.*;

public class ThreeDeeController
{
	private static Hashtable<Integer,Phantom3DCamera> cameras = null;
	private static String DEFAULT_CAMERA_NAME = "Static Camera";
	private static int DEFAULT_CAMERA = -1;

	public static Phantom3DCamera getCamera( int nodeId )
	{
		if( cameras == null )
			initCameras();
		
		if( nodeId == DEFAULT_CAMERA )
			return null;//this is interpreted as setNominalViewingTransform() and field of view 60 in Phantom3DUniverse

		return cameras.get( new Integer( nodeId ) );
	}

	public static String getCameraName( int  nodeId )
	{
		if( nodeId == -1 )
			return DEFAULT_CAMERA_NAME;

		if( cameras == null )
			return "cameras == null";

		Phantom3DCamera camera = getCamera( nodeId );
		return camera.getName();
	}

	public static void addCamera( RenderNode cameraNode )
	{
		if( cameras == null )
			initCameras();
		cameras.put( new Integer( cameraNode.getID() ), (Phantom3DCamera) cameraNode.getImageOperation() );
	}

	public static void setNewCameraName( Phantom3DCamera camera )
	{
		if( cameras == null )
			initCameras();

		int maxCreateIndex = 0;
		Collection values = cameras.values();
		Iterator i = values.iterator();
		while( i.hasNext() )
		{
			Phantom3DCamera c = (Phantom3DCamera) i.next();
			int createIndex = c.createIndex.get();
			if( createIndex > maxCreateIndex )
				maxCreateIndex = createIndex;
		}
		maxCreateIndex++;

		camera.createIndex.set( maxCreateIndex );
		String newName = "3D Camera " + ( new Integer( maxCreateIndex ) ).toString();
		camera.setName( newName );
	}

	public static int getNewCameraID( int currentCameraID )
	{

		Iterator keysI = cameras.keySet().iterator();
		Vector<Integer> keys = new Vector<Integer>();
		while( keysI.hasNext() )
		{
			keys.add( (Integer)  keysI.next() );
		}

		String[] options = new String[ keys.size() + 1 ];
		options[ 0 ] = DEFAULT_CAMERA_NAME;
		for( int i = 0; i < keys.size(); i++ )
		{
			Integer camKey = keys.elementAt( i );
			Phantom3DCamera c = cameras.get( camKey.intValue() );
			options[ i + 1 ] = c.getName();
		}

		final MComboBox camerasCombo = new MComboBox( "Camera for Node", 75, options );

		MInputArea mArea = new MInputArea( "Camera" );
		mArea.add( camerasCombo );

		final MInputPanel pPanel = new MInputPanel( "Select Camera" );
		pPanel.add( mArea );

		int retVal = DialogUtils.showMultiInput( pPanel );

		if( retVal != DialogUtils.OK_OPTION ) 
			return currentCameraID;

		int index = camerasCombo.getSelectedIndex();
		if( index == 0 )
			return DEFAULT_CAMERA;

		Integer newID = keys.elementAt( index - 1 );
		return newID.intValue();
	}

	private static void initCameras()
	{
		cameras = new  Hashtable<Integer,Phantom3DCamera>();
		Vector<RenderNode> nodes = ProjectController.getFlow().getNodes();
		for( RenderNode node  : nodes )
		{
			if( node.getImageOperation() instanceof Phantom3DCamera )
				cameras.put( new Integer( node.getID() ), (Phantom3DCamera) node.getImageOperation() );
		}
	}

	//--- Return true is none of the nodfes contain cameras that used buy some 3D sources.
	public static boolean isOkayToDeleteNodes( Vector<RenderNode> nodes )
	{
		return true;//not impl
	}

	public static Phantom3DLightScene getLightScene( int index )
	{
		return null;
	}

}//end class